package top.jolyoulu.core.acc

import org.apache.spark.rdd.RDD
import org.apache.spark.util.LongAccumulator
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author: JolyouLu
 * @Date: 2024/2/6 16:32
 * @Description
 */
object Spark01_RDD_Acc {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("Acc")
    val sc: SparkContext = new SparkContext(sparkConf)

    val rdd: RDD[Int] = sc.makeRDD(List(1, 2, 3, 4))
    //获取系统累加器,Spark默认提供的简单累加
    val sumAcc: LongAccumulator = sc.longAccumulator("sum")

    rdd.foreach(num => {
      sumAcc.add(num)
    })
    //获取累计器值
    println(sumAcc.value)

    //关闭环境
    sc.stop()
  }
}
